// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "refund_user_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "退款用户",
    "label": "退款用户"
  },
  "refund_from_transaction_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "支付方平台订单号",
    "label": "支付方平台订单号"
  },
  "refund_order_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "退款单号",
    "label": "退款单号"
  },
  "refund_total_amount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "double"
      }
    ],
    "title": "退款金额",
    "label": "退款金额"
  },
  "order_total_amount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "double"
      }
    ],
    "title": "订单总金额",
    "label": "订单总金额"
  },
  "refund_remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "退款备注",
    "label": "退款备注"
  },
  "operator_id": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "string"
      }
    ],
    "title": "操作人员",
    "label": "操作人员"
  },
  "pay_platform": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 0,
            "text": "微信支付"
          }
        ]
      }
    ],
    "title": "支付平台",
    "defaultValue": 0,
    "label": "支付平台"
  },
  "refund_status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 0,
            "text": "正在退款"
          },
          {
            "value": 1,
            "text": "退款成功"
          },
          {
            "value": 2,
            "text": "退款异常"
          },
          {
            "value": 3,
            "text": "退款关闭"
          }
        ]
      }
    ],
    "title": "退款状态",
    "defaultValue": 0,
    "label": "退款状态"
  },
  "refund_module": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "food-order-list",
            "text": "美食外卖"
          },
          {
            "value": "admin-order-list",
            "text": "快递代取"
          }
        ]
      }
    ],
    "title": "退款模块",
    "defaultValue": "food-order-list",
    "label": "退款模块"
  }
}

const enumConverter = {
  "pay_platform_valuetotext": {
    "0": "微信支付"
  },
  "refund_status_valuetotext": {
    "0": "正在退款",
    "1": "退款成功",
    "2": "退款异常",
    "3": "退款关闭"
  },
  "refund_module_valuetotext": {
    "food-order-list": "美食外卖",
    "admin-order-list": "快递代取"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
